package com.itheima.app.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.exception.BusinessException;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.utils.JwtUtil;
import com.itheima.vo.SettingVo;
import com.itheima.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/18
 */
@RestController
public class UserController {

    @Autowired
    private UserManager userManager;

    //添加用户
    @PostMapping("/save")
    public Long save(@RequestBody User user) {
        return userManager.save(user);
    }

    //根据手机号查询用户
    @GetMapping("/findByPhone")
    public User findByPhone(String phone) {
        return userManager.findByPhone(phone);
    }

    //发送验证码
    @PostMapping("/user/login")
    public void sendSms(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        userManager.sendSms(phone);
    }

    //登录或者注册
    @PostMapping("/user/loginVerification")
    public Map regOrLog(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        String verificationCode = map.get("verificationCode");

        return userManager.regOrLog(phone, verificationCode);
    }

    //解析token
    public User parseToken(String token) {
        //解析token形成map
        Map map = JwtUtil.parseToken(token);
        //将map转化为对象
        User user = BeanUtil.mapToBean(map, User.class, true);
        return user;
    }

    //完善个人信息
    @PostMapping("/user/loginReginfo")
    public void logRegInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {
        //校验token 解析token
        //User user = parseToken(token);
        User user = UserHolder.get();
        //赋值用户的id
        userInfo.setId(user.getId());
        //保存用户对象
        userManager.saveUserInfo(userInfo);
    }

    //完善个人信息-头像上传
    @PostMapping({"/user/loginReginfo/head", "/users/header"})
    public void uploadHeader(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //校验token 解析token
        //User user = parseToken(token);
        User user = UserHolder.get();
        //保存用户对象
        userManager.updateUserInfo(headPhoto, user.getId());
    }

    //根据用户id查询个人信息
    @GetMapping("/users")
    public UserInfoVo findByUser(@RequestHeader("Authorization") String token, Long userID, Long huanxinID) {
        //根据token查询用户对象
        //User user = parseToken(token);
        User user = UserHolder.get();
        UserInfoVo userInfoVo = null;
        if (huanxinID != null) {
            userInfoVo = userManager.findByUserId(huanxinID);
        } else if (user.getId() != null) {
            userInfoVo = userManager.findByUserId(user.getId());
        } else if (userID != null) {
            userInfoVo = userManager.findByUserId(userID);
        }

        return userInfoVo;
    }

    //修改用户的个人信息
    @PutMapping("/users")
    public void updateUserInfo(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo) {
        userManager.updateUserInfo(userInfo);
    }

    //修改手机号- 1 发送短信验证码
    @PostMapping("/users/phone/sendVerificationCode")
    public void sendVerificationCode(@RequestHeader("Authorization") String token) {
        userManager.sendSms(UserHolder.get().getPhone());
    }

    //修改手机号 - 2 校验验证码
    @PostMapping("/users/phone/checkVerificationCode")
    public Map checkVerificationCode(@RequestBody Map<String, String> map) {
        //1.获取验证码
        String verificationCode = map.get("verificationCode");
        //2.调manager
        return userManager.checkVerificationCode(verificationCode);

    }

    //修改手机号 - 3 保存
    @PostMapping("/users/phone")
    public void updatePhone(@RequestBody Map<String, String> map, @RequestHeader("Authorization") String token) throws BusinessException, BusinessException {
        //1.获取新手机号
        String phone = map.get("phone");
        //2.调manager
        userManager.updatePhone(phone);
        //3.删除原账号token
        userManager.deleteToken(token);
    }


}
